from torch import Tensor
import torch
import numpy as np

# 将浮点数组量化为Q8.8定点模式
# 表示范围 : [-128.99609375, 127.99609375]
def float_tensor2Q88(t: Tensor)-> Tensor:
    t = t.float()
    # assert (torch.max(t) <= 127.99609375 and torch.min(t) >= -128.99609375)
    return (t*256).round().clone().detach().int()

def Q882float_tensor(t: Tensor)-> Tensor:
    t = t.int()
    t = (t / 256.)
    assert (torch.max(t) <= 127.99609375 and torch.min(t) >= -128.99609375)
    return torch.tensor(t, dtype=torch.int16)
